#include "JoystickManager.h"
#include <cstring>
#include "Common.h"

JoystickManager::JoystickManager()
{
}

JoystickManager::~JoystickManager()
{
}

void JoystickManager::Update()
{
	memcpy(&m_oPreviousStates, &m_oCurrentStates, sizeof(JoystickState) * JOYSTICKMANAGER_JOYSTICK_MAX);

	for(int joystick = GLFW_JOYSTICK_1; joystick < JOYSTICKMANAGER_JOYSTICK_MAX; joystick++)
	{
		m_oCurrentStates[joystick] = JoystickState::GetState(joystick);
	}
}

float JoystickManager::GetAxis(int joystick, int axis) const
{
	return m_oCurrentStates[joystick].GetAxis(axis);
}

float JoystickManager::GetAxisChange(int joystick, int axis) const
{
	return m_oCurrentStates[joystick].GetAxis(axis) - m_oPreviousStates[joystick].GetAxis(axis);
}

bool JoystickManager::IsButtonDown(int joystick, int button) const
{
	return m_oCurrentStates[joystick].IsButtonDown(button);
}

bool JoystickManager::IsButtonUp(int joystick, int button) const
{
	return m_oCurrentStates[joystick].IsButtonUp(button);
}

bool JoystickManager::IsButtonPressed(int joystick, int button) const
{
	return m_oPreviousStates[joystick].IsButtonUp(button) && m_oCurrentStates[joystick].IsButtonDown(button);
}

bool JoystickManager::IsButtonReleased(int joystick, int button) const
{
	return m_oPreviousStates[joystick].IsButtonDown(button) && m_oCurrentStates[joystick].IsButtonUp(button);
}

JoystickManager* JoystickManager::GetInstance()
{
	if(!m_pInstance)
	{
		m_pInstance = new JoystickManager();
	}

	return m_pInstance;
}

JoystickManager* JoystickManager::m_pInstance = NULL;